package examination;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import examination.util.IdUtil;

/**
 * Servlet implementation class ExamApplyServlet
 */
@WebServlet("/ExamApplyServlet")
public class ExamApplyServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ExamApplyServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * 申请考试。接收考试所有字段作为参数，调用ExamBean的相关方法对数据库进行修改。
	 * 刚申请的考试状态设置为“待审核”
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		/**表单参数
		 * Date date;//考试日期
			String time;//考试时间，未格式化文本
			String courseNo;//课程号
			String[] classNo;//参加考试的班级号列表
			String location;//考试地点
			int workload;//监考工分(等效于监考工作量)
			int need;//所需副监考人数
			String comment;//备注，补充说明
			 
		 */
		
		try {
			request.setCharacterEncoding("UTF-8");
			String dateString = request.getParameter("date");
			String time = request.getParameter("time");
			String courseNo = request.getParameter("courseNo");
			String[] classNoList = request.getParameterValues("classNo");
			String location = request.getParameter("location");
			String workloadString = request.getParameter("workload");
			String needString = request.getParameter("need");
			String comment = request.getParameter("comment");
			
			//若参数不足则跳转回去，comment不是必填项不需要检查
			if(dateString==null||time==null||courseNo==null||
					classNoList==null||location==null||workloadString==null||
					needString==null) {
				//response.sendRedirect(request.getHeader("Referer"));
				request.getRequestDispatcher(request.getHeader("Referer")).forward(request, response);
			}
			
			//处理以及获取表单无法获得的参数
			String examNo = IdUtil.createID();//随机生成ID
			Date applyDate = new Date();//为今天的日期	
			String status = "待审核";
			
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Date date;
			date = sdf.parse(dateString);
			String termNo = ExamBean.getTermNo(date);
			
			int workload = Integer.valueOf(workloadString);
			int need = Integer.valueOf(needString);
			
			AccountBean account = (AccountBean)request.getSession().getAttribute("account");
			
			String applierNo = account.getStaffNo();
			
			//初始化考试对象
			ExamBean exam = new ExamBean();
			exam.setExamNo(examNo);
			exam.setTerm(termNo);
			exam.setCourseNo(courseNo);
			exam.setWorkload(workload);
			exam.setDate(date);
			exam.setLocation(location);
			exam.setTime(time);
			exam.setNeed(need);
			exam.setStatus(status);
			exam.setComment(comment);
			exam.setApplierNo(applierNo);
			exam.setApplyDate(applyDate);
			exam.setClassNo(classNoList);
			
			ExamBean.addExam(exam);
			response.sendRedirect("exam_info.jsp");
			
		} catch (Exception e) {
			e.printStackTrace();
			//response.sendRedirect(request.getHeader("Referer"));
			//request.getRequestDispatcher(request.getHeader("Referer")).forward(request, response);
		}
	
	}
	
}
